A character LCD has been an interesting staff for novice micro-controller learner. Prior to the present of C programming for micro-controller, the student used the Assembly language to program this character LCD. It requires a few dozens of Assembly codes.
Simulating Program In Proteus Using The STM32F103R6 |
Its original LCD controller was the Hitachi HD44780 chip. Currently it was replace by many other equivalent controller chip with low cost. For example, a 16x2 character LCD module costs around 1$.
A typical character LCD module |
A typical character LCD module |
Its Pins Diagram |
The interface between the micro-controller and this character LCD is a conventional parallel 8-bit data mode. However this LCD controller allow the program to use a 4-bit data transfer mode. You can see this tutorial for the LCD interface using 8-bit mode.
The LCD module is readable/writable. But we just write the command/data to this module is OK. The Register Select (RS) pin allow us to choose between command (RS=0) and data (RS=1). The Enable (E) pin allow the parallel data lath into the controller whenever there is a transition from High To Low.
I use the STM32CubeIDE to write this code. It's very easy to use and it's free of charge. We need to register first before it allows us to download this IDE.
I select a number of output pin in code configuration wizard before it can generate the core source codes.
STM32F103R6 Pin Configuration In STM32CubeIDE |
I need to write a few C functions to send the command and data to the LCD. And then I need to initialize the LCD module.
/* USER CODE BEGIN Header */ /** ****************************************************************************** * @file : main.c * @brief : Main program body ****************************************************************************** * @attention * * <h2><center>© Copyright (c) 2022 STMicroelectronics. * All rights reserved.</center></h2> * * This software component is licensed by ST under BSD 3-Clause license, * the "License"; You may not use this file except in compliance with the * License. You may obtain a copy of the License at: * opensource.org/licenses/BSD-3-Clause * ****************************************************************************** */ /* USER CODE END Header */ /* Includes ------------------------------------------------------------------*/ #include "main.h" /* Private function prototypes -----------------------------------------------*/ void SystemClock_Config(void); static void MX_GPIO_Init(void); /** * @brief The application entry point. * @retval int */ int main(void) { /* USER CODE BEGIN 1 */ /* USER CODE END 1 */ /* MCU Configuration--------------------------------------------------------*/ /* Reset of all peripherals, Initializes the Flash interface and the Systick. */ HAL_Init(); /* USER CODE BEGIN Init */ /* USER CODE END Init */ /* Configure the system clock */ SystemClock_Config(); /* Initialize all configured peripherals */ MX_GPIO_Init(); lcdInit(); lcdGotoXy(1,1); lcdPrint("STM32CubeIDE and"); lcdGotoXy(1,2); lcdPrint("STM32F103R6 LCD"); /* USER CODE BEGIN WHILE */ while (1) { } } void delay(uint16_t num){ for(uint16_t i=0;i<num;i++); } void lcdCommand(uint16_t cmd){ GPIOB->ODR=cmd<<2; //GPIOB->ODR&=~(1<<RS_Pin); HAL_GPIO_WritePin(RS_GPIO_Port,RS_Pin,GPIO_PIN_RESET); //GPIOB->ODR|=(1<<EN_Pin); HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_SET); //HAL_Delay(1); delay(100); //GPIOB->ODR&=~(1<<EN_Pin); HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET); //HAL_Delay(10); delay(10000); } void lcdData(uint8_t data){ GPIOB->ODR=data<<2; //GPIOB->ODR|=(1<<RS_Pin); HAL_GPIO_WritePin(RS_GPIO_Port,RS_Pin,GPIO_PIN_SET); //GPIOB->ODR|=(1<<EN_Pin); HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_SET); //HAL_Delay(1); delay(10); //GPIOB->ODR&=~(1<<EN_Pin); HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET); //HAL_Delay(10); delay(1000); } void lcdInit(void){ //GPIOB->ODR&=~(1<<RS_Pin); HAL_GPIO_WritePin(EN_GPIO_Port,EN_Pin,GPIO_PIN_RESET); //HAL_Delay(2); delay(20000); lcdCommand(0x38); lcdCommand(0x0F); lcdCommand(0x01); //HAL_Delay(20); delay(20000); lcdCommand(0x06); } void lcdGotoXy(unsigned char x,unsigned char y){ unsigned char charAddr[]={0x80,0xC0,0x94,0xD4}; lcdCommand(charAddr[y-1]+x-1); //HAL_Delay(1); delay(1000); } void lcdPrint(char *str){ unsigned char i=0; while(str[i]!=0){ lcdData(str[i]); i++; } } /** * @brief System Clock Configuration * @retval None */ void SystemClock_Config(void) { RCC_OscInitTypeDef RCC_OscInitStruct = {0}; RCC_ClkInitTypeDef RCC_ClkInitStruct = {0}; /** Initializes the RCC Oscillators according to the specified parameters * in the RCC_OscInitTypeDef structure. */ RCC_OscInitStruct.OscillatorType = RCC_OSCILLATORTYPE_HSI; RCC_OscInitStruct.HSIState = RCC_HSI_ON; RCC_OscInitStruct.HSICalibrationValue = RCC_HSICALIBRATION_DEFAULT; RCC_OscInitStruct.PLL.PLLState = RCC_PLL_NONE; if (HAL_RCC_OscConfig(&RCC_OscInitStruct) != HAL_OK) { Error_Handler(); } /** Initializes the CPU, AHB and APB buses clocks */ RCC_ClkInitStruct.ClockType = RCC_CLOCKTYPE_HCLK|RCC_CLOCKTYPE_SYSCLK |RCC_CLOCKTYPE_PCLK1|RCC_CLOCKTYPE_PCLK2; RCC_ClkInitStruct.SYSCLKSource = RCC_SYSCLKSOURCE_HSI; RCC_ClkInitStruct.AHBCLKDivider = RCC_SYSCLK_DIV1; RCC_ClkInitStruct.APB1CLKDivider = RCC_HCLK_DIV1; RCC_ClkInitStruct.APB2CLKDivider = RCC_HCLK_DIV1; if (HAL_RCC_ClockConfig(&RCC_ClkInitStruct, FLASH_LATENCY_0) != HAL_OK) { Error_Handler(); } } /** * @brief GPIO Initialization Function * @param None * @retval None */ static void MX_GPIO_Init(void) { GPIO_InitTypeDef GPIO_InitStruct = {0}; /* GPIO Ports Clock Enable */ __HAL_RCC_GPIOB_CLK_ENABLE(); __HAL_RCC_GPIOA_CLK_ENABLE(); /*Configure GPIO pin Output Level */ HAL_GPIO_WritePin(GPIOB, RS_Pin|EN_Pin|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 |GPIO_PIN_8|GPIO_PIN_9, GPIO_PIN_RESET); /*Configure GPIO pins : RS_Pin EN_Pin PB2 PB3 PB4 PB5 PB6 PB7 PB8 PB9 */ GPIO_InitStruct.Pin = RS_Pin|EN_Pin|GPIO_PIN_2|GPIO_PIN_3 |GPIO_PIN_4|GPIO_PIN_5|GPIO_PIN_6|GPIO_PIN_7 |GPIO_PIN_8|GPIO_PIN_9; GPIO_InitStruct.Mode = GPIO_MODE_OUTPUT_PP; GPIO_InitStruct.Pull = GPIO_NOPULL; GPIO_InitStruct.Speed = GPIO_SPEED_FREQ_LOW; HAL_GPIO_Init(GPIOB, &GPIO_InitStruct); } /* USER CODE BEGIN 4 */ /* USER CODE END 4 */ /** * @brief This function is executed in case of error occurrence. * @retval None */ void Error_Handler(void) { /* USER CODE BEGIN Error_Handler_Debug */ /* User can add his own implementation to report the HAL error return state */ __disable_irq(); while (1) { } /* USER CODE END Error_Handler_Debug */ } #ifdef USE_FULL_ASSERT /** * @brief Reports the name of the source file and the source line number * where the assert_param error has occurred. * @param file: pointer to the source file name * @param line: assert_param error line source number * @retval None */ void assert_failed(uint8_t *file, uint32_t line) { /* USER CODE BEGIN 6 */ /* User can add his own implementation to report the file name and line number, ex: printf("Wrong parameters value: file %s on line %d\r\n", file, line) */ /* USER CODE END 6 */ } #endif /* USE_FULL_ASSERT */ /************************ (C) COPYRIGHT STMicroelectronics *****END OF FILE****/
Click here to download its source file.
For other similar posts please check,
- Getting Started With STM32F103C8T6 Module with STM32CubeIDE
- STM32F103C8T6 Blue Pill SysTick and Multiplexing Display Example
- STM32F103C8T6 Blue Pill Switch And Multiplexing Display Interface Using SysTick
- STM32F103C8T6 Blue Pill SysTick LED Blinking
- STM32F103R6 Common Anode Seven Segments Display Example
- STM32F103R6 Common Anode Seven Segments Display And Switch Interfacing
- STM32F103R6 Simple 2-Digit Multiplexing Display And Switch Example
- STM32F103R6 SysTick And Digital Clock Example
- STM32F103R6 SysTick Two-Digit Multiplexing Display and Push Button
- LED Blinking With STM32F103R6 Using SysTick
- STM32F103R6 SPI Interfaces To SN74HC595N Shift Registers
- STM32F103R6 GPIO Interfaces To A Character LCD In 8-Bit Mode
- STM32F103R6 SPI Interfaces To A Single Seven-Segment Display
No comments:
Post a Comment